// 日程数据相关时间计算工具类
var moment = require('moment');
var _ = require('underscore');
var config = require('../conf/config');
var scheduleDate = require('./scheduleDate');
var period = config.constant.period;
var queryTypeConstant = config.constant.queryType;

module.exports =    function(){

    var computeBySize = function(schedules, pointTime, queryType, size, isFirstPage){
        var duplicate = [];
        var nextTime = null;

        _.each(schedules, function(schedule){
            nextTime = scheduleDate.recalculateEntryPointTime(schedule.timeScope.start, pointTime, schedule.repeat, queryType, isFirstPage);
            // 创建一个副本加上和成时间放入生成数组中
            duplicate.push(_.extend({pointTime : nextTime}, schedule));
            var deadline = moment(schedule.deadline);
            deadline.set('hour', '23');
            deadline.set('minute', '59');
            deadline.set('second', '59');

            for (var i = 0; i < size - 1; i++) {
                nextTime = scheduleDate.computeNextTimeByType(nextTime, schedule.repeat, queryType);
                if (queryType == queryTypeConstant.NEXT && moment(nextTime).diff(deadline) > 0) break;
                if (queryType == queryTypeConstant.BACK && moment(nextTime).diff(moment(schedule.timeScope.start)) < 0) break;
                duplicate.push(_.extend({pointTime : nextTime}, schedule));
            }
        });

        return duplicate;
    };

    return {
        computeBySize : computeBySize
    };
}();
